﻿using Newtonsoft.Json;
using ZZDingTalkSDK.ZZDingTalkRequest.Common;
using ZZDingTalkSDK.ZZDingTalkResponse;
using ZZDingTalkSDK.ZZDingTalkResponse.common;

namespace ZZDingTalkSDK.ZZDingTalkRequest
{
    /// <summary>
    /// 发送消息请求
    /// </summary>
    public class ChatSendMsgRequest : IZZDRequest<ZZDingTalkResponseContent<ChatSendMsgResponse>>
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        [JsonProperty("tenantId")]
        public string? TenantId { get; set; }

        [JsonProperty("chatType")]
        public int ChatType { get; set; }

        /// <summary>
        /// 消息体（需要将Message对象序列化为json字符串）
        /// </summary>
        [JsonProperty("msg")]
        public string? Msg { get; set; }

        /// <summary>
        /// 发送者用户ID
        /// </summary>
        [JsonProperty("senderId")]
        public string? SenderId { get; set; }

        /// <summary>
        /// 单聊接受者用户id（chatType为1时必填）
        /// </summary>
        [JsonProperty("receiverId")]
        public string? ReceiverId { get; set; }

        /// <summary>
        /// 群聊会话id（chatType为2时必填）
        /// </summary>
        public string? ChatId { get; set; }

        public string GetApiName()
        {
            return "/chat/sendMsg";
        }
    }

    /// <summary>
    /// 消息内容
    /// </summary>
    public class Message
    {
        /// <summary>
        /// 消息类型：["text", "link", "image", "previewFile"]
        /// </summary>
        [JsonProperty("msgtype")]
        public string? MsgType { get; set; }

        [JsonProperty("text")]
        public TextContent? Text { get; set; }

        [JsonProperty("link")]
        public LinkContent? Link { get; set; }

        [JsonProperty("image")]
        public ImageContent? Image { get; set; }

        [JsonProperty("previewFile")]
        public PreviewFileContent? PreviewFile { get; set; }

        /// <summary>
        /// 调用此方法自动更新消息类型
        /// </summary>
        public void Done()
        {
            if (Text != null)
            {
                MsgType = "text";
            }
            if (Link != null)
            {
                MsgType = "link";
            }
            if (Image != null)
            {
                MsgType = "image";
            }
            if (PreviewFile != null)
            {
                MsgType = "previewFile";
            }
        }

        /// <summary>
        /// 序列化对象为json字符串
        /// </summary>
        /// <returns></returns>
        public string ToJson()
        {
            Done();
            return JsonConvert.SerializeObject(this);
        }
    }

    /// <summary>
    /// 文本消息内容
    /// </summary>
    public class TextContent
    {
        /// <summary>
        /// 文本消息内容（最多3000字符）
        /// </summary>
        [JsonProperty("content")]
        public string? Content { get; set; }
    }

    /// <summary>
    /// 连接消息内容
    /// </summary>
    public class LinkContent
    {
        /// <summary>
        /// 标题（最多50字符）
        /// </summary>
        [JsonProperty("title")]
        public string? Title { get; set; }

        /// <summary>
        /// 描述（最多500字符）
        /// </summary>
        [JsonProperty("text")]
        public string? Text { get; set; }

        /// <summary>
        /// 点击跳转的链接
        /// </summary>
        [JsonProperty("messageUrl")]
        public string? MessageUrl { get; set; }

        /// <summary>
        /// 图片的mediaId
        /// </summary>
        [JsonProperty("picMediaId")]
        public string? PicMediaId { get; set; }

        /// <summary>
        /// 来源地址（非必填）
        /// </summary>
        [JsonProperty("sourceUrl")]
        public string? SourceUrl { get; set; }
    }

    /// <summary>
    /// 图片消息内容
    /// </summary>
    public class ImageContent
    {
        /// <summary>
        /// 图片的mediaId
        /// </summary>
        [JsonProperty("mediaId")]
        public string? MediaId { get; set; }

        /// <summary>
        /// 图片文件类型：0: jpg，1: gif，2: png，3: bpm
        /// </summary>
        [JsonProperty("fileType")]
        public int FileType { get; set; }

        /// <summary>
        /// 图片大小
        /// </summary>
        [JsonProperty("picSize")]
        public string? PicSize { get; set; }
    }

    /// <summary>
    ///  文件消息内容
    /// </summary>
    public class PreviewFileContent
    {
        /// <summary>
        /// 文件medialId
        /// </summary>
        [JsonProperty("mediaId")]
        public string? MediaId { get; set; }

        /// <summary>
        /// 文件类型（比如docx）
        /// </summary>
        [JsonProperty("fileType")]
        public string? FileType { get; set; }

        /// <summary>
        /// 文件大小
        /// </summary>
        [JsonProperty("size")]
        public string? Size { get; set; }

        /// <summary>
        /// 文件名-带文件类型
        /// </summary>
        [JsonProperty("name")]
        public string? Name { get; set; }
    }
}